package com.example.JavaWebApi.core.commonmodel;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @PROJECT_NAME: JavaWebApi
 * @DESCRIPTION: 通用返回模型
 * @Author: YangZe
 * @DATE: 2021/11/28 14:51
 */
@Data
@ApiModel("通用返回结果")
public class ResponseModel<T> {
    public ResponseModel(){

    }

    /**
     *
     * @param fail  是否为失败信息，是则items赋值给message
     * @param responseTypeEnum
     * @param items
     */
    public ResponseModel(Boolean fail, ResponseTypeEnum responseTypeEnum, T items){
        if (responseTypeEnum.getCode() == 2002 || responseTypeEnum.getCode() == 5000 && fail) {
            setCode(responseTypeEnum.getCode());
            setMessage((String) items);
            setItems(items);
        } else {
            setCode(responseTypeEnum.getCode());
            setMessage(responseTypeEnum.getDescription());
            setItems(items);
        }

    }

    public ResponseModel(ResponseTypeEnum responseTypeEnum, String message) {
        setCode(responseTypeEnum.getCode());
        setMessage(message);
    }

    public ResponseModel(ResponseTypeEnum responseTypeEnum, T items) {
        setCode(responseTypeEnum.getCode());
        setMessage(responseTypeEnum.getDescription());
        setItems(items);
    }

    public ResponseModel(T items) {
        if (items instanceof Boolean) {
            Boolean res = (Boolean) items;
            if (res) {
                setCode(ResponseTypeEnum.OperationSuccess.getCode());
                setMessage(ResponseTypeEnum.OperationSuccess.getDescription());
            }
            else {
                setCode(ResponseTypeEnum.OperationFailed.getCode());
                setMessage(ResponseTypeEnum.OperationFailed.getDescription());
            }
        }
        else {
            setCode(ResponseTypeEnum.Success.getCode());
            setMessage(ResponseTypeEnum.Success.getDescription());
        }
        setItems(items);
    }
    @ApiModelProperty("操作结果码")
    private int code;
    @ApiModelProperty("操作结果信息")
    private String message;
    @ApiModelProperty("真正返回结果值")
    private T items;
}
